package com.yjt.fresh.activity;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.yjt.fresh.R;
import com.yjt.fresh.base.BaseSimpleActivity;
import com.yjt.fresh.constant.MyConstants;
import com.yjt.fresh.utils.CommonsToolsHelper;
import com.yjt.fresh.utils.PreferenceManager;
import com.yjt.fresh.widget.ClearEditText;
import com.yjt.fresh.widget.WarpLinearLayout;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 搜索页
 *
 * @author mcc
 * @date 2018/6/15
 */

public class AtySearch extends BaseSimpleActivity {
    @BindView(R.id.rl_app_aty_search_back)
    RelativeLayout mRlBack;
    @BindView(R.id.edt_app_aty_search)
    ClearEditText mEdtSearch;
    @BindView(R.id.btn_app_aty_search_search)
    Button mBtnSearch;
    @BindView(R.id.tv_app_aty_search_product)
    TextView mTvProduct;
    @BindView(R.id.v_app_aty_search_product)
    View mVProduct;
    @BindView(R.id.rl_app_aty_search_product)
    RelativeLayout mRlProduct;
    @BindView(R.id.tv_app_aty_search_store)
    TextView mTvStore;
    @BindView(R.id.v_app_aty_search_store)
    View mVStore;
    @BindView(R.id.rl_app_aty_search_store)
    RelativeLayout mRlStore;
    @BindView(R.id.wllAppAtySearch)
    WarpLinearLayout mWll;
    @BindView(R.id.tvAppAtySearchClearHistory)
    TextView mTvClearHistory;
    @BindView(R.id.rlAppAtySearchClear)
    RelativeLayout mRlClear;
    @BindView(R.id.tvAtySearchNoHistory)
    TextView mTvNoHistory;

    private int mProductOrStore = MyConstants.PRODUCT;

    @Override
    protected int getContentView() {
        return R.layout.app_aty_search;
    }

    @Override
    protected void initViews() {
        ButterKnife.bind(this);
        CommonsToolsHelper.setEditTextInputSpace(mEdtSearch);
        showHistory();
    }

    private void showHistory() {
        PreferenceManager preferenceManager = PreferenceManager.getInstance();
        mWll.removeAllViews();
        if (MyConstants.PRODUCT == mProductOrStore) {
            String productSearchHistory = preferenceManager.getProductSearchHistory();
            if (!TextUtils.isEmpty(productSearchHistory)) {
                mTvNoHistory.setVisibility(View.GONE);
                String[] productSearchHistories = productSearchHistory.split(",");
                for (int i = 0; i < productSearchHistories.length; i++) {
                    if (TextUtils.isEmpty(productSearchHistories[i])) {
                        continue;
                    }
                    TextView tv = (TextView) View.inflate(mContext, R.layout.app_warpll_item_search_history, null);
                    tv.setText(productSearchHistories[i]);
                    tv.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            handleProductSearch(PreferenceManager.getInstance(),((TextView)v).getText().toString());
                        }
                    });
                    mWll.addView(tv);
                }
            }

        } else {
            String storeSearchHistory = preferenceManager.getStoreSearchHistory();
            if (!TextUtils.isEmpty(storeSearchHistory)) {
                mTvNoHistory.setVisibility(View.GONE);
                String[] storeSearchHistories = storeSearchHistory.split(",");
                for (int i = 0; i < storeSearchHistories.length; i++) {
                    if (TextUtils.isEmpty(storeSearchHistories[i])) {
                        continue;
                    }
                    TextView tv = (TextView) View.inflate(mContext, R.layout.app_warpll_item_search_history, null);
                    tv.setText(storeSearchHistories[i]);
                    tv.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            handleStoreSearch(PreferenceManager.getInstance(),((TextView)v).getText().toString());
                        }
                    });
                    mWll.addView(tv);
                }
            }
        }
        if (mWll.getChildCount() > 0) {
            mTvClearHistory.setVisibility(View.VISIBLE);
            mTvNoHistory.setVisibility(View.GONE);
        } else {
            mTvClearHistory.setVisibility(View.GONE);
            mTvNoHistory.setVisibility(View.VISIBLE);
        }
    }

    @Override
    protected void initEvent() {
        initClick();
        //输入框没有值 不可以搜索 必须在点击事件之后设置
        mBtnSearch.setClickable(false);
        mBtnSearch.setBackgroundResource(R.drawable.shape_f0f0f0_100);
    }

    private void initClick() {
        mEdtSearch.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.toString().length() > 0){
                    mBtnSearch.setClickable(true);
                    mBtnSearch.setBackgroundResource(R.drawable.selector_43b624_3b9f20_100);
                }else {
                    mBtnSearch.setClickable(false);
                    mBtnSearch.setBackgroundResource(R.drawable.shape_f0f0f0_100);
                }
            }
        });
        mRlBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        mBtnSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PreferenceManager preferenceManager = PreferenceManager.getInstance();
                String keyWord = mEdtSearch.getText().toString();
                if (mProductOrStore == MyConstants.PRODUCT) {
                    handleProductSearch(preferenceManager, keyWord);
                } else if (mProductOrStore == MyConstants.STORE) {
                    handleStoreSearch(preferenceManager, keyWord);
                }
            }
        });
        mRlProduct.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mProductOrStore = MyConstants.PRODUCT;
                showHistory();
                mRlProduct.setBackgroundColor(getResources().getColor(R.color.color_f2faf0));
                mTvProduct.setTextColor(getResources().getColor(R.color.color_43b624));
                mVProduct.setVisibility(View.VISIBLE);

                mRlStore.setBackgroundColor(getResources().getColor(R.color.white));
                mTvStore.setTextColor(getResources().getColor(R.color.color_333333));
                mVStore.setVisibility(View.GONE);
                mEdtSearch.setHint("搜索想要的商品、品牌");
            }
        });

        mRlStore.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mProductOrStore = MyConstants.STORE;
                showHistory();
                mRlStore.setBackgroundColor(getResources().getColor(R.color.color_f2faf0));
                mTvStore.setTextColor(getResources().getColor(R.color.color_43b624));
                mVStore.setVisibility(View.VISIBLE);

                mRlProduct.setBackgroundColor(getResources().getColor(R.color.white));
                mTvProduct.setTextColor(getResources().getColor(R.color.color_333333));
                mVProduct.setVisibility(View.GONE);
                mEdtSearch.setHint("搜索想看的店铺");
            }
        });

        mTvClearHistory.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final Dialog dialog = new Dialog(mContext, R.style.MyDialog);
                dialog.setContentView(R.layout.app_dialog_sure_cancel);
                TextView tvCancel = dialog.findViewById(R.id.tvAppDialogSureCancelCancel);
                TextView tvSure = dialog.findViewById(R.id.tvAppDialogSureCancelSure);
                tvCancel.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (dialog != null && dialog.isShowing()) {
                            dialog.dismiss();
                        }
                    }
                });
                tvSure.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mWll.removeAllViews();
                        mTvClearHistory.setVisibility(View.GONE);
                        mTvNoHistory.setVisibility(View.VISIBLE);
                        if (mProductOrStore == MyConstants.PRODUCT) {
                            PreferenceManager.getInstance().clearProductSearchHistory();
                        } else {
                            PreferenceManager.getInstance().clearStoreSearchHistory();
                        }
                        if (dialog != null && dialog.isShowing()) {
                            dialog.dismiss();
                        }
                    }
                });
                dialog.show();
            }
        });
    }

    private void handleProductSearch(PreferenceManager preferenceManager, String keyWord) {
        String productSearchHistory = preferenceManager.getProductSearchHistory();
        String[] productSearchHistories = productSearchHistory.split(",");
        boolean isContain = false;
        for (int i = 0; i < productSearchHistories.length; i++) {
            if (keyWord.equals(productSearchHistories[i])) {
                isContain = true;
                break;
            }
        }
        if (!TextUtils.isEmpty(keyWord) && !isContain) {
            productSearchHistory = productSearchHistory + "," + keyWord;
            preferenceManager.setProductSearchHistory(productSearchHistory);
        }
        Intent intent = new Intent(mContext, AtyProducts.class);
        intent.putExtra(MyConstants.PRODUCT_KEY_WORD, keyWord.trim());
        intent.putExtra(MyConstants.SEARCH_OR_CATEGORY,MyConstants.SEARCH);
        startActivity(intent);
        finish();
    }

    private void handleStoreSearch(PreferenceManager preferenceManager, String keyWord) {
        String storeSearchHistory = preferenceManager.getStoreSearchHistory();
        String[] storeSearchHistories = storeSearchHistory.split(",");
        boolean isContain = false;
        for (int i = 0; i < storeSearchHistories.length; i++) {
            if (keyWord.equals(storeSearchHistories[i])) {
                isContain = true;
                break;
            }
        }
        if (!TextUtils.isEmpty(keyWord) && !isContain) {
            storeSearchHistory = storeSearchHistory + "," + keyWord;
            preferenceManager.setStoreSearchHistory(storeSearchHistory);
        }
        Intent intent = new Intent(mContext, AtyStores.class);
        intent.putExtra(MyConstants.STORE_KEY_WORD, keyWord.trim());
        startActivity(intent);
        finish();
    }

}
